<?php
if ( !defined('IN_SITE') ){
	die('Hacking Attemp!');
}

//function image_resize($file, $dimension, $resize_type = 1, $quality = 100) {
//function image_create_thumbnail($src, $dest, $dimension, $resize_type = 1, $quality = 100) {
//Resize type = 1 (resize width and height)
//Resize type = 2 (resize only width)
//Resize type = 3 (resize only height)

define('CHMOD_FILES', 0666);
define('CHMOD_DIRS', 0777);


function image_resize($file, $dimension, $resize_type = 1, $quality = 100) {
$types = array(1 => "gif", 2 => "jpeg", 3 => "png");
	$image_info = getimagesize($file);
	if (!$image_info) { return false; }

	$file_bak = $file.".bak";
	if (!rename($file, $file_bak)) { return false; }

	$width_height = image_get_width_height($dimension, $image_info[0], $image_info[1], $resize_type);

	$thumb = ImageCreateTrueColor($width_height['width'], $width_height['height']);
	$image_create_handle = "imagecreatefrom".$types[$image_info[2]];
	if ($image = $image_create_handle($file_bak)) {
		imagecopyresized($thumb, $image, 0, 0, 0, 0, $width_height['width'], $width_height['height'], ImageSX($image), ImageSY($image));
		$image_handle = "image".$types[$image_info[2]];
		$image_handle($thumb, $file, $quality);
		imagedestroy($image);
		imagedestroy($thumb);
		@chmod($file, CHMOD_FILES);
	    @unlink($file_bak);
	    return true;
	}
	else {
		rename($file_bak, $file);
		return false;
	}
}

function image_resize_width_height($file, $max_width, $max_height, $quality = 100) {
	$types		= array(1 => "gif", 2 => "jpeg", 3 => "png");

	//Check file and max dimension
	if ( !file_exists($file) || (!$max_width && !$max_height) ){		
		return false;
	}
	$image_info	= getimagesize($file);
 
	//Resize width or height
	$resize_type	= ($max_width && ($image_info[0] > $max_width)) ? 2 : 0; //2: resize width
	$resize_type	+= ($max_height && ($image_info[1] > $max_height)) ? 3 : 0; //3: resize height
	
	if ($resize_type == 5){ //5: resize both width and height
		if (($image_info[0]/$image_info[1] > $max_width/$max_height)){
			$resize_type	= 2; //Only resize width
		}else{
			$resize_type	= 3; //Only resize height
		}
	}

	if ( $resize_type == 0 ){
		return true; //Don't need to resize
	}

	//Backup original file
	$file_bak	= $file.".bak";
	if ( !rename($file, $file_bak) ){
		return false;
	}

	//Calculate new width, height
	$dimension		= ($resize_type == 2) ? $max_width : $max_height;
	//Lấy kich thuc moi cua hình anh
	$width_height	= image_get_width_height($dimension, $image_info[0], $image_info[1], $resize_type);

	$thumb = ImageCreateTrueColor($width_height['width'], $width_height['height']);
	$image_create_handle = "imagecreatefrom".$types[$image_info[2]];
	
	if ($image = $image_create_handle($file_bak)) {
		imagecopyresized($thumb, $image, 0, 0, 0, 0, $width_height['width'], $width_height['height'], ImageSX($image), ImageSY($image));
		$image_handle = "image".$types[$image_info[2]];
		$image_handle($thumb, $file, $quality);
		imagedestroy($image);
		imagedestroy($thumb);
		@chmod($file, CHMOD_FILES);
	    @unlink($file_bak);
	    return true;
	}else{
		rename($file_bak, $file);
		return false;
	}
}

function image_create_thumbnail($src, $dest, $dimension, $resize_type = 1, $quality = 100) {
	$types	= array(1 => "gif", 2 => "jpeg", 3 => "png");

	if (file_exists($dest)) { @unlink($dest); }
	$image_info = getimagesize($src);
	if (!$image_info) { return false; }

	$width_height = image_get_width_height($dimension, $image_info[0], $image_info[1], $resize_type);

	$thumb = imagecreatetruecolor($width_height['width'], $width_height['height']);
	$image_create_handle = "imagecreatefrom".$types[$image_info[2]];
	if ($image = $image_create_handle($src)) {
		imagecopyresized($thumb, $image, 0, 0, 0, 0, $width_height['width'], $width_height['height'], ImageSX($image), ImageSY($image));
		$image_handle = "image".$types[$image_info[2]];
		$image_handle($thumb, $dest, $quality);
		imagedestroy($image);
		imagedestroy($thumb);
		@chmod($dest, CHMOD_FILES);
	}
	return (file_exists($dest)) ? true : false;
}
function image_get_width_height($dimension, $width, $height, $resize_type) {
	if ($resize_type == 2) {
		$new_width = $dimension;
		$new_height = floor(($dimension/$width) * $height);
	}elseif ($resize_type == 3) {
		$new_width = floor(($dimension/$height) * $width);
		$new_height = $dimension;
	}else {
		$ratio = $width / $height;
		if ($ratio > 1) {
			$new_width = $dimension;
			$new_height = floor(($dimension/$width) * $height);
		}else {
			$new_width = floor(($dimension/$height) * $width);
			$new_height = $dimension;
		}
	}
	return array("width" => $new_width, "height" => $new_height);
}

?>